package demo.gojapn.translationdemo;

import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.RawRes;

import java.io.IOException;

import demo.gojapn.translationdemo.common.view.ProgressWheel;

/**
 * Created by x.chen on 2015/10/15 0015.
 */
public class MediaPlayerService {

    private MediaPlayer player;
    private ProgressWheel wheel;
    private Handler mHandler = new Handler(){

        @Override
        public void dispatchMessage(Message msg) {
            if (wheel != null ){

                wheel.setProgress(player.getCurrentPosition() *1.0f/ player.getDuration());
            }
        }
    };

    public MediaPlayerService(Context c,@RawRes int id ) {
        player  = MediaPlayer.create(c, id);
        if (player == null) {
            AssetFileDescriptor fileDesc =c. getResources().openRawResourceFd(id);
            if (fileDesc != null) {
                player = new MediaPlayer();
                try {
                     player.setDataSource(fileDesc.getFileDescriptor(), fileDesc.getStartOffset(), fileDesc.getLength());
                    fileDesc.close();
                } catch (IOException e) {
                   throw  new NullPointerException("ResId " +id +" not found");
                }
                if (player == null) {
                        throw  new NullPointerException("ResId " +id +" not found");
                }
            }
        }


        final AudioManager audioManager = (AudioManager)c. getSystemService(Context.AUDIO_SERVICE);
        if (audioManager.getStreamVolume(AudioManager.STREAM_MUSIC) == 0) {
            audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC)/2 , 0);
        }

        player.setLooping(false);
        try {
            player.prepare();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                wheel.setProgress(1);
                player.seekTo(0);
                player.pause();
                wheel.setProgress(0);
            }
        });
        final int milliseconds = 100;
        new Thread(){
            @Override
            public void run(){
                while(true){
                    try {
                        sleep(milliseconds);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    mHandler.sendEmptyMessage(0);
                }
            }
        }.start();
    }

    public void setWheel(ProgressWheel wheel) {
        this.wheel = wheel;

    }

    public void playOrPause() {
        if (player == null) return;

        if (player.isPlaying()){
            player.pause();
        }else {
            player.start();




        }
    }


}

